package com.littlesandbox.mryj;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends Activity
{
    public TextView t;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t =(TextView)findViewById(R.id.text);
    }

    public void getSentence(View view)
    {
        //https://cloud.tencent.com/developer/article/1667338
        OkHttpClient httpClient = new OkHttpClient();

        String url = "https://v1.hitokoto.cn";
        Request getRequest = new Request.Builder()
                .url(url)
                .get()
                .build();

        Call call = httpClient.newCall(getRequest);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //同步请求，要放到子线程执行
                    Response response = call.execute();
//                    String result = response.body().string();
//                    Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG);
                    String result = response.body().toString();
                    Gson gson = new Gson();
                    JsonParser parser = new JsonParser();
                    JsonElement jsonElement = parser.parse(result);
                    JsonObject jsonObject = jsonElement.getAsJsonObject();

                    String hitokotoText = jsonObject.get("hitokoto").getAsString();
                    Log.i("TAG", "okHttpGet run: response:"+ response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
//        String reuslt = sendGet(" https://v1.hitokoto.cn/");
//        t.setText(reuslt);
//    }
//    psublic static String sendGet(String url) {
//        StringBuilder result = new StringBuilder();
//        URL obj = null;
//        HttpsURLConnection con = null;
//        try {
//            obj = new URL(url);
//            // 注意这里我们使用HttpsURLConnection
//            con = (HttpsURLConnection) obj.openConnection();
//
//            // 可选的请求头
//            con.setRequestMethod("GET");
//
//            // 读取响应
//            int responseCode = con.getResponseCode();
//            System.out.println("\nSending 'GET' request to URL : " + url);
//            System.out.println("Response Code : " + responseCode);
//
//            BufferedReader in = new BufferedReader(
//                    new InputStreamReader(con.getInputStream()));
//            String inputLine;
//            StringBuffer response = new StringBuffer();
//
//            while ((inputLine = in.readLine()) != null) {
//                response.append(inputLine);
//            }
//            in.close();
//
//            // 打印结果
//            result.append(response.toString());
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (con != null) {
//                con.disconnect();
//            }
//        }
//        return result.toString();
//    }

}
